﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Imaginecup.util.Funtion;
using Imaginecup.recycling.Stage;
using Imaginecup.Recycling.GameMan;
using Imaginecup.shareData;
using Imaginecup.factory;
using Imaginecup.gameState;
using Imaginecup.recycling.GameMan;
using Imaginecup.gameState.recycling.Engine;
using Imaginecup.recycling.Object;
using Imaginecup.gameState.recycling.GameMan;
using Imaginecup.recycling.Factory;

namespace Imaginecup.Engine
{
    public class TileMap
    {
        public const int TileWidth = 48;
        public const int TileHeight = 48;
        public static int MapWidth = 160;
        public static int MapHeight = 12;

        private static MapInfo[,] MapSquares;
        private static MenuManager InventoryManager;
        private static IManager CurrentManager;

        public static void ChangeStage_Init(IStageInfo stage)
        {
            Camera.WorldRectangle = new Rectangle(0, 0, stage.MapWidth, stage.MapHeight);
            MapSquares = stage.MapSquares;
            MapWidth = stage.MapWidth / TileWidth;
            MapHeight = stage.MapHeight / TileHeight;
        }

        public static int GetSquareByPixelX(int pixelX)
        {
            return pixelX / TileWidth;
        }
        public static int GetSquareByPixelY(int pixelY)
        {
            return pixelY / TileHeight;
        }
        public static Vector2 GetSquareAtPixel(Vector2 pixelLocation)
        {
            return new Vector2(
                GetSquareByPixelX((int)pixelLocation.X),
                GetSquareByPixelY((int)pixelLocation.Y));
        }
        public static Vector2 GetSquareCenter(int squareX, int squareY)
        {
            return new Vector2(
                (squareX * TileWidth) + (TileWidth / 2),
                (squareY * TileHeight) + (TileHeight / 2));
        }
        public static Vector2 GetSquareCenter(Vector2 squrare)
        {
            return GetSquareCenter(
                (int)squrare.X,
                (int)squrare.Y);
        }
        public static Rectangle SqurareWorldRectangle(int x, int y)
        {
            return new Rectangle(
                x * TileWidth,
                y * TileHeight,
                TileWidth,
                TileHeight
                );
        }
        public static Rectangle SqurareWorldRectangle(Vector2 squrare)
        {
            return SqurareWorldRectangle((int)squrare.X, (int)squrare.Y);
        }
        public static Rectangle SquareScreenRectangle(int x, int y)
        {
            return Camera.WorldToScreen(SqurareWorldRectangle(x, y));
        }
        public static Rectangle SquareScreenRectangle(Vector2 square)
        {
            return SquareScreenRectangle((int)square.X, (int)square.Y);
        }

        public static MapState GetTileAtSquareState(int tileX, int tileY)
        {
            if ((tileX >= 0) && (tileX < MapWidth)
                && (tileY >= 0) && (tileY < MapHeight))
            {
                if (MapSquares[tileX, tileY] != null)
                    return MapSquares[tileX, tileY].m_State;
                else
                    return MapState.NONE;
            }
            else
                return MapState.NONE;
        }

        public static MapState GetTileAtPixel(int pixelX, int pixelY)
        {
            return GetTileAtSquareState(
                GetSquareByPixelX(pixelX),
                GetSquareByPixelY(pixelY));
        }

        public static MapState GetTileAtPixel(Vector2 pixelLocation)
        {
            return GetTileAtSquareState(
                (int)pixelLocation.X,
                (int)pixelLocation.Y);
        }

        public static bool IsWallTile(int tileX, int tileY)
        {
            MapState tileIndex = GetTileAtSquareState(tileX, tileY);

            switch (tileIndex)
            {
                case MapState.NONE:
                    return false;
                case MapState.CAN:
                case MapState.PAPER:
                case MapState.PLASTIC:
                case MapState.GLASS:
                case MapState.ETC:
                    String name = tileIndex.ToString();
                    InventoryManager.GroupButton.GetObserver(name).SetValue(5);
                    MapSquares[tileX, tileY] = null;
                    //break;
                    return false;
                case MapState.END:
                    CurrentManager.SetGameClear(true);
                    break;
                case MapState.DIE:
                    CurrentManager.SetGameOver(true);
                    break;
            }

            return true;
        }
        public static bool IsWallTile(Vector2 square)
        {
            return IsWallTile((int)square.X, (int)square.Y);
        }
        public static bool IsWallTileByPixel(Vector2 pixelLocation)
        {
            return IsWallTile(
                GetSquareByPixelX((int)pixelLocation.X),
                GetSquareByPixelY((int)pixelLocation.Y));
        }

        public static void Draw(SpriteBatch spriteBatch)
        {
            int startX = GetSquareByPixelX((int)Camera.Position.X);
            int endX = GetSquareByPixelX((int)Camera.Position.X + Camera.ViewportWidth);

            int startY = GetSquareByPixelY((int)Camera.Position.Y);
            int endY = GetSquareByPixelY((int)Camera.Position.Y + Camera.ViewportHeight);

            for (int x = startX; x <= endX; x++)
                for (int y = startY; y <= endY; y++)
                {
                    if ((x >= 0) && (y >= 0) && (x < MapWidth) && (y < MapHeight))
                    {
                        if (GetTileAtSquareState(x, y) == MapState.NONE || GetTileAtSquareState(x, y) == MapState.START)
                        {
                            continue;
                        }
                        spriteBatch.Begin();
                        if( MapSquares[x, y].m_FileName != "")
                        {
                            spriteBatch.Draw(
                            ResourceManager.GetInstance().GetContent(MapSquares[x, y].m_FileName),
                            SquareScreenRectangle(x, y),
                                //mapSquares2[x, y].m_Position,
                            MapSquares[x, y].m_DrawRect,
                            Color.White);
                        }
                        spriteBatch.End();
                    }
                }
        }

        internal static void SetItemManager(MenuManager inventoryManager)
        {
            TileMap.InventoryManager = inventoryManager;
        }
        internal static void SetCurrentStage(IManager m_StageManager)
        {
            TileMap.CurrentManager = m_StageManager;
        }

        
    }
}
